package no.monsen.webservice;

import no.monsen.webservice.commons.MonsenFact;
import no.monsen.webservice.db.MonsenDAO;

import org.apache.axis2.context.MessageContext;
import org.apache.log4j.Logger;

/**
 * 
 * @author geir.k.wollum 28.02.2008
 * 
 */
public class MonsenService {
	private Logger log = Logger.getLogger(MonsenService.class.getName());

	public MonsenFact getMonsenFact(MonsenFact request) {
		MessageContext c = MessageContext.getCurrentMessageContext();
		MonsenDAO dao = MonsenDAO.getInstance();
		MonsenFact response = null;

		log.info("Klient ID [" + request.getClientID() + "] fra IP ["
				+ c.getFrom().getAddress() + "]");

		if (request.getClientID().equals("0")
				|| request.getClientID().length() <= 8) {
			response = new MonsenFact();
			response.setFact("Monsen sier at det er en ny klient tilgjengelig");
			log.info("Klient ID [" + request.getClientID() + "] maa oppdateres");
		} else {
			dao.addVisitor(request);
			response = dao.getMonsenFact();
		}

		return response;
	}

	public String isUpdateAvailable(String version) {
		MonsenDAO dao = MonsenDAO.getInstance();

		String thisVersion = dao.checkVersion(version);
		log.info("Klient versjon [" + version + "], Monsen versjon ["
				+ thisVersion + "]");

		return thisVersion;
	}
}
